//
//  DFHomeViewController.swift
//  新浪微博
//
//  Created by dafei on 16/5/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SVProgressHUD

private let HomeCellId = "HomeCellId"

class DFHomeViewController: DFBaseTableViewController {
    
    //定义模型数组
//    lazy var statues: [DFStatus] = [DFStatus]()
    //添加视图模型属性
    lazy var statuesListViewModel: DFStatusLIstViewModel = DFStatusLIstViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //用户未为登录  设置访客视图的信息
        if !userLogin {
        visitorLoginView.updateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)
            return
        }
        
        setNavBar()
        
        prepareForTableView()
        
        loadData()

    }
    
    private func prepareForTableView() {
    
        //注册tableView
        tableView.registerClass(DFStatuesCell.self, forCellReuseIdentifier: HomeCellId)
        //设置cell 的高度
        tableView.rowHeight = 300
        
        //给刷新控件实例化
        refreshControl = UIRefreshControl()
        //添加触发响应的事件
        refreshControl?.addTarget(self, action: #selector(DFHomeViewController.loadData), forControlEvents: .ValueChanged)
   
    }
    
    
    
    //设置导航条
    private func setNavBar() {
    
        let popItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(DFHomeViewController.push))
        
        navigationItem.rightBarButtonItem = popItem
    
    }
    
    @objc private func push(){
        
        let detail = DFDetailViewController()
        
        detail.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(detail, animated: true)
   
    
    }
    
}

extension DFHomeViewController {
    //加载赋值
    @objc private func loadData() {
        statuesListViewModel.loadData { (isSuccess) in
            
            //停止小菊花转动
            self.refreshControl?.endRefreshing()
            
            if !isSuccess {
            
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                
                return
            
            }
                 //刷新数据
            self.tableView.reloadData()
            
        }
 
    }

}

//实现数据源方法
extension DFHomeViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuesListViewModel.statuesViewModelArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //以后获取缓存cell 都是使用这种fangfa
        //需要注册cell 类型 和可重用标识
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! DFStatuesCell
        
//        cell.textLabel?.text = statues[indexPath.row].user?.name
        cell.statuesViewModel = statuesListViewModel.statuesViewModelArray[indexPath.row]
        
        return cell
        
    }







}











    

